from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from django.urls import re_path
from app01 import consumers

websocket_urlpatterns = [
    # 跟阿里物联网连接
    re_path(r'room/(?P<group>\w+)/$', consumers.ChatConsumer.as_asgi()),
    # 折线图实时连接
    # re_path(r'api/device-data/$', consumers.DeviceDataConsumer.as_asgi()),
    re_path(r'^api/device-data/(?:\?.*)?$', consumers.DeviceDataConsumer.as_asgi())  # (?:\?.*)? 表示可选匹配?及其后任意字符
]

# 用于根据请求的协议类型（HTTP/HTTPS 或 WebSocket）将请求分发给不同的处理模块,不加只会分发http/https,不会WebSocket
application = ProtocolTypeRouter({
    "websocket": AuthMiddlewareStack(URLRouter(websocket_urlpatterns)),
})
# application = ProtocolTypeRouter({
#     "websocket": URLRouter(websocket_urlpatterns)
# })
